package com.chorye.mongo.service;

import com.chorye.mongo.dao.primary.PrimaryRepository;
import com.chorye.mongo.dao.primary.PrimaryStudent;
import com.chorye.mongo.dao.secondary.SecondaryRepository;
import com.chorye.mongo.dao.secondary.SecondaryStudent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Company: 北京XXXX有限公司
 * @Author JiangYy
 * @Title: StudentService
 * @Description: TODO
 * @Date 2018/12/24 14:32
 */
@Service
@Slf4j
public class StudentService {

    @Autowired
    private PrimaryRepository primaryRepository;
    @Autowired
    private SecondaryRepository secondaryRepository;

    /**
     * 保存学生
     *
     * @param primaryStudent
     * @param secondaryStudent
     */
    public void save(PrimaryStudent primaryStudent, SecondaryStudent secondaryStudent) {
        PrimaryStudent student1 = primaryRepository.insert(primaryStudent);
        log.info(student1.toString());
        SecondaryStudent student2 = secondaryRepository.insert(secondaryStudent);
        log.info(student2.toString());
    }

    /**
     * 删除学生
     *
     * @param name
     * @return
     */
    public void delete(String name) {
        primaryRepository.deleteById(1);
        secondaryRepository.deleteById(1);
    }

    /**
     * 修改学生
     *
     * @param primaryStudent
     * @param secondaryStudent
     */
    public void update(PrimaryStudent primaryStudent, SecondaryStudent secondaryStudent) {
        PrimaryStudent student1 = primaryRepository.save(primaryStudent);
        log.info(student1.toString());
        SecondaryStudent student2 = secondaryRepository.save(secondaryStudent);
        log.info(student2.toString());
    }

    /**
     * 查找学生
     *
     * @param name 学生姓名
     * @return
     */
    public void find(String name) {
        PrimaryStudent student1 = primaryRepository.findByName(name);
        log.info(student1.toString());
        SecondaryStudent student2 = secondaryRepository.findByName(name);
        log.info(student2.toString());
    }

    /**
     * 查找学生集合
     *
     * @return 学生集合
     */
    public void findAll() {
        List<PrimaryStudent> primaryRepositoryAll = primaryRepository.findAll();
        log.info(primaryRepositoryAll.toString());
        List<SecondaryStudent> secondaryRepositoryAll = secondaryRepository.findAll();
        log.info(secondaryRepositoryAll.toString());
    }
}
